<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
          <a-col :xl='24' :lg='7' :md='8' :sm='24'>
            <a-radio-group v-model='queryParam.propertyStatus' @change='radioChange'>
              <a-form-item label='项目属性'>
                <a-radio :value='0'>在建项目</a-radio>
                <a-radio :value='1'>全部</a-radio>
              </a-form-item>
            </a-radio-group>
          </a-col>
        </a-row>
        <a-row :gutter='24'>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='项目编号'>
              <a-input placeholder='请输入项目编号' v-model='queryParam.code'></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <a-form-item label='项目简称'>
              <a-input placeholder='请输入项目简称' v-model='queryParam.shortName'></a-input>
            </a-form-item>
          </a-col>
          <template v-if='toggleSearchStatus'>
            <!--            <a-col :xl="6" :lg="7" :md="8" :sm="24">-->
            <!--              <a-form-item label="项目属性">-->
            <!--                <j-dict-select-tag placeholder="请选择项目属性" v-model="queryParam.projectProperty"-->
            <!--                                   dictCode="project_property"/>-->
            <!--              </a-form-item>-->
            <!--            </a-col>-->
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='项目分类'>
                <j-dict-select-tag placeholder='请选择项目分类' v-model='queryParam.type'
                                   dictCode='icm_project_type' />
              </a-form-item>
            </a-col>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='是否延期'>
                <j-dict-select-tag placeholder='请选择项目状态' v-model='queryParam.isDefer'
                                   dictCode='switch_status' />
              </a-form-item>
            </a-col>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='项目状态'>
                <j-dict-select-tag placeholder='请选择项目状态' v-model='queryParam.projectState'
                                   dictCode='hf_project_states' />
              </a-form-item>
            </a-col>
            <a-col :xl='6' :lg='7' :md='8' :sm='24'>
              <a-form-item label='项目经理'>
                <a-input placeholder='请输入项目经理' v-model='queryParam.charger'></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl='6' :lg='7' :md='8' :sm='24'>
            <span style='float: left;overflow: hidden;' class='table-page-search-submitButtons'>
              <a-button type='primary' @click='searchQuery' icon='search'>查询</a-button>
              <a-button type='primary' @click='searchReset' icon='reload' style='margin-left: 8px'>重置</a-button>
              <a @click='handleToggleSearch' style='margin-left: 8px'>
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button @click='handleAdd' type='primary' icon='plus'>新增</a-button>
      <a-button type='primary' icon='download' @click="handleExportXls('项目管理')">导出</a-button>
      <a-upload name='file' :showUploadList='false' :multiple='false' :headers='tokenHeader' :action='importExcelUrl'
                @change='handleImportExcel'>
        <a-button type='primary' icon='import'>导入</a-button>
      </a-upload>
      <a-button @click='handlePlan' type='primary' icon='setting'>规划</a-button>
      <a-dropdown v-if='selectedRowKeys.length > 0'>
        <a-menu slot='overlay'>
          <a-menu-item key='1' @click='batchDel'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'> 批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
    </div>
    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a style='font-weight: 600'>{{
          selectedRowKeys.length
        }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        :scroll='{x:true}'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        class='j-table-force-nowrap'
        @change='handleTableChange'>
        <template slot='custShortName' slot-scope='text, record'>
          <a @click='handleDetail(record)'>{{ text }}</a>
        </template>
        <template slot='estimateWorkday' slot-scope='text, record'>
                    <span v-if='judgeProjectWorkHours(record)' style='color: red'>
                      {{ record.projectWorkHours + '/' + text }}
                    </span>
          <span v-else>
                      {{ record.projectWorkHours + '/' + text }}
                    </span>
        </template>
        <template slot='actualWorkday' slot-scope='text, record'>
                    <span v-if='judgeProjectWorkHours2(record)' style='color: red'>
                      {{ record.projectWorkHoursCharger + '/' + text }}
                    </span>
          <span v-else>
                      {{ record.projectWorkHoursCharger + '/' + text }}
                    </span>
        </template>
        <template slot='isDefer_dictText' slot-scope='text,record'>
          <a-tag v-if='record.isDefer==="Y"' style='margin-right: 0' color='red'>{{ text }}</a-tag>
          <a-tag v-if='record.isDefer==="N"' style='margin-right: 0' color='green'>{{ text }}</a-tag>
        </template>
        <template slot='delayDays' slot-scope='text,record'>
          <span v-if='text == null || text ==0'>
            {{ text }}
          </span>
          <span v-else>
<!--            {{ text }}-->
             <a style='color: red' @click='handleRisk(record)'>{{ text }}</a>
          </span>
        </template>
        <template slot='projectState_dictText' slot-scope='text'>
          <span v-if="text=='已延期'" style='color: red'>
            {{ text }}
          </span>
          <span v-else>
            {{ text }}
          </span>
        </template>
        <template slot='imgSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无图片</span>
          <img v-else :src='getImgView(text)' height='25px' alt=''
               style='max-width:80px;font-size: 12px;font-style: italic;' />
        </template>
        <template slot='fileSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无文件</span>
          <a-button
            v-else
            :ghost='true'
            type='primary'
            icon='download'
            size='small'
            @click='downloadFile(text)'>
            下载
          </a-button>
        </template>
        <span slot='action' slot-scope='text, record'>
          <a @click='handleEdit(record)' v-if='record.projectState!=="04"'>编辑</a>
          <a-divider type='vertical' v-if='record.projectState!=="04"' />

          <a-dropdown>
            <a class='ant-dropdown-link'>更多 <a-icon type='down' /></a>
            <a-menu slot='overlay'>
               <a-menu-item>
                <a @click='handleDetail(record)'>详情</a>
              </a-menu-item>
              <a-menu-item v-if='record.projectState!=="04"'>
                <a-popconfirm title='确定删除吗?' @confirm='() => handleDelete(record.id)'>
                  <a style='color:red'>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <a-menu-item v-if='record.projectState!=="04"'>
                <a @click='handleChangeStatus(record) '>验收</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
          <a-divider type='vertical'/>
           <a @click='handShowProject(record) '><span>{{record.showProject==="Y" ?  '展示中' : '未展示'}}
                </span></a>
        </span>
      </a-table>
    </div>

    <pm-project-modal ref='modalForm' @ok='modalFormOk'></pm-project-modal>
    <hf-pm-project-modal ref='hfPmProjectModal' @pm-project-select='pmProjectSelect'></hf-pm-project-modal>
    <hf-pm-project-meeting-records-modal ref='hfPmProjectMeetingRecordsModal'></hf-pm-project-meeting-records-modal>
    <pm-project-risk-list-model ref='pmProjectRiskListModel'></pm-project-risk-list-model>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import PmProjectModal from './modules/PmProjectModal'
import { getAction } from '@/api/manage'
import HfPmProjectModal from '../meeting/modules/HfPmProjectModal'
import HfPmProjectMeetingRecordsModal from '../meeting/modules/HfPmProjectMeetingRecordsModal'
import PmProjectRiskListModel from '@views/hifar/pm/modules/PmProjectRiskListModel'

import { filterObj } from '@/utils/util'
import { sorter } from '@/utils/sorter'

export default {
  name: 'PmProjectList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    PmProjectModal,
    HfPmProjectModal,
    HfPmProjectMeetingRecordsModal,
    PmProjectRiskListModel

  },
  data() {
    return {
      isorter: {
        column: 'signDate',
        order: 'desc'
      },
      description: '项目管理管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '项目编号',
          align: 'center',
          dataIndex: 'code',
          sorter: sorter('code'),
          scopedSlots: { customRender: 'custShortName' }
        },
        // {
        //   title: '合同编号',
        //   align: 'center',
        //   dataIndex: 'numCode'
        // },
        {
          title: '项目简称',
          align: 'center',
          dataIndex: 'shortName',
          sorter: sorter('shortName')
        },
        {
          title: '项目状态',
          align: 'center',
          dataIndex: 'projectState_dictText',
          scopedSlots: { customRender: 'projectState_dictText' }
        },
        {
          title: '项目经理',
          align: 'center',
          dataIndex: 'charger'
        },
        {
          title: '开发人天',
          align: 'center',
          dataIndex: 'estimateWorkday',
          scopedSlots: { customRender: 'estimateWorkday' }
        },
        {
          title: '实施人天',
          align: 'center',
          dataIndex: 'actualWorkday',
          scopedSlots: { customRender: 'actualWorkday' }
        },
        // {
        //   title: '实际人天',
        //   align: 'center',
        //   dataIndex: 'projectWorkHours',
        //   scopedSlots: { customRender: 'projectWorkHours' },
        // },
        // {
        //   title: '项目经理实际人天',
        //   align: 'center',
        //   dataIndex: 'projectWorkHoursCharger',
        //   // scopedSlots: { customRender: 'projectWorkHours' }
        // },
        {
          title: '完工比例',
          align: 'center',
          dataIndex: 'actualPersonDay',
          scopedSlots: { customRender: 'actualPersonDay' }
        },
        {
          title: '计划开始时间',
          align: 'center',
          dataIndex: 'planStartTime',
          sorter: sorter('planStartTime'),
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '计划结束时间',
          align: 'center',
          dataIndex: 'planEndTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '实际结束时间',
          align: 'center',
          dataIndex: 'checkTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        // {
        //   title: '是否延期',
        //   align: 'center',
        //   dataIndex: 'isDefer_dictText',
        //   scopedSlots: { customRender: 'isDefer_dictText' }
        // },
        {
          title: '延期天数',
          align: 'center',
          dataIndex: 'delayDays',
          scopedSlots: { customRender: 'delayDays' }
        },
        // {
        //   title: '项目得分',
        //   align: 'center',
        //   dataIndex: 'projectScore'
        // },
        {
          title: '签订日期',
          align: 'center',
          dataIndex: 'signDate'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/pm/pmProject/list',
        delete: '/pm/pmProject/delete',
        deleteBatch: '/pm/pmProject/deleteBatch',
        exportXlsUrl: '/pm/pmProject/exportXls',
        importExcelUrl: 'pm/pmProject/importExcel',
        changeStatus: 'pm/pmProject/changeStatus',
        showProject: 'pm/pmProject/showProject'

      },
      queryModel: true,
      queryParam: {
        propertyStatus: 0
      }

    }
  },
  created() {
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handShowProject(record) {
      if (!this.url.showProject) {
        this.$message.error('请设置url.showProject属性!')
      }
      getAction(this.url.showProject, record).then((res) => {
        if (res.success) {
          this.loadData()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleChangeStatus(record) {
      if (!this.url.changeStatus) {
        this.$message.error('请设置url.changeStatus属性!')
      }
      getAction(this.url.changeStatus, record).then((res) => {
        if (res.success) {
          this.loadData()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handlePlan() {
      if (this.selectedRowKeys.length === 1) {
        this.$router.push({
          path: '/hifar/pm/HfPmImplementationManage',
          query: { id: JSON.stringify(this.selectedRowKeys) }
        })
      } else {
        this.$message.warning('最多选择一条')
      }
      // this.$nextTick(() => {
      //   this.$refs.hfPmProjectMeetingRecordsModal.title = '测试页面弹出，该处功能暂未添加'
      //   this.$refs.hfPmProjectMeetingRecordsModal.visible = true
      //   alert('测试页面弹出，该处功能暂未添加')
      // })
    },
    searchReset() {
      this.queryParam = {}
      this.queryParam.propertyStatus = 0
      this.loadData(1)
    },
    radioChange() {
      this.loadData()
    },
    handleDetail: function(record) {
      record.clickArchive = false // 为了识别是点击了编辑或详情
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.disableSubmit = true
    },
    handleRisk: function(record) {
      this.$nextTick(() => {
        this.$refs.pmProjectRiskListModel.queryRisk(record)
        this.$refs.pmProjectRiskListModel.title = '项目风险'
      })
    },
    pmProjectSelect(data) {
      alert(data.id + '-->' + data.shortName)
    },
    // 判断延期天数
    judgeProjectWorkHours(record) {
      let a = parseFloat(record.projectWorkHours)
      let b = parseFloat(record.estimateWorkday)
      if (a > b) {
        return true
      } else {
        return false
      }
    },
    judgeProjectWorkHours2(record) {
      let a = parseFloat(record.projectWorkHoursCharger)
      let b = parseFloat(record.actualWorkday)
      if (a > b) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>